package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/11
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.subimt(orders);
     /*   LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //根据用户id查询查当前购物车信息,集合里面有每个菜的 菜品的id,套餐id口味,用户id
        BigDecimal money = new BigDecimal(0);
        //获取购物车数据
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);

        //遍历这个购物车,计算总金额
        for (ShoppingCart shoppingCart : list) {
            Integer number = shoppingCart.getNumber();
            BigDecimal moneys = new BigDecimal(number);
            BigDecimal amount = shoppingCart.getAmount();
              money = money.add(amount.multiply(moneys));
        }

        //下单用户的id,地址,下单时间,结账时间,支付方式,实收金额
        // 用户名,手机号,地址,收货人保存到订单表
        orders.setUserId(BaseContext.getCurrentId());
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setAmount(money);
        orders.setStatus(1);
        //根据地址id查询地址数据
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getId,orders.getAddressBookId());
        AddressBook addressBook = addressBookService.getOne(wrapper);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());

        orders.setConsignee(addressBook.getConsignee());

        orders.setNumber(String.valueOf(orders.getId()));
        //保存信息到订单表
        ordersService.save(orders);

        //我要将下单的具体信息保存到订单明细表
        //订单id,口味,数量,总金额,图片,菜品id,套餐id
      List<OrderDetail>  orderlist = list.stream().map((item)->{
          OrderDetail detail = new OrderDetail();
          //设置菜品名称
          detail.setName(item.getName());
          //设置订单id
          detail.setOrderId(orders.getId());
          //设置口味名称
          String dishFlavorname = item.getDishFlavor();
          detail.setDishFlavor(dishFlavorname);
          //设置数量
          detail.setNumber(item.getNumber());
          //设置总金额  数量*金额
          BigDecimal bigDecimal = new BigDecimal(item.getNumber());
          detail.setAmount(bigDecimal.multiply(item.getAmount()));
          //设置图片
          String image = item.getImage();
          detail.setImage(image);
          //设置菜品id
          Long dishId = item.getDishId();
          detail.setDishId(dishId);
          //设置套餐id
          Long setmealId = item.getSetmealId();
          detail.setSetmealId(setmealId);
        return detail;
      }).collect(Collectors.toList());
        orderDetailService.saveBatch(orderlist);

             //清除购物车
            shoppingCartService.remove(lambdaQueryWrapper);
*/
        return R.success("添加成功");
    }
}
